<template>
  <div>
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确定"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <div style="padding: 10px;">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="50"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </div>
  </div>
</template>
<script>
import { updateUser } from '@/api/user.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  created() {
    this.message = this.value
  },
  computed: {},
  data() {
    return { message: '' }
  },
  methods: {
    async onClickRight() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.message.trim() === '') return this.$toast('请输入昵称')
        await updateUser({
          name: this.localNmae
        })
        this.$emit('input', this.message)
        this.$emit('close')
        this.$toast('更新成功')
      } catch (err) {
        console.dir(err)
        if (err.response && err.response.status === 409) {
          return this.$toast('用户名已存在')
        }
        this.$toast('更新失败')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-nav-bar {
    /deep/ .van-nav-bar__content {
      background-color: #fff !important;
      .van-nav-bar__title {
        color: #323233 !important;
      }
    }
  }

  .van-field {
    padding-top: 10px;
  }
}
</style>
